//
//  Util.m
//  VietNews
//
//  Created by Nguyen .a.Hph on 5/4/12.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#import "Util.h"

@implementation Util
+(NSString *)getBaseUrl:(NSString *)url{
    if ([url rangeOfString:@"http://"].location == NSNotFound)
        return nil;
    NSRange pos = [url rangeOfString:@"/" options:NSCaseInsensitiveSearch range:NSMakeRange(8, [url length]-8)];
    if (pos.location!=NSNotFound)
        return [url substringToIndex:pos.location];
    return url;
}
+(NSString *)getAbsoluteUrl:(NSString *)url parentUrl:(NSString *)parentUrl{
    if ([url rangeOfString:@"http://"].length>0 || [url rangeOfString:@"#"].location==0)
        return url;
    NSString *hostname = [self getBaseUrl:parentUrl];
    
    if ([url indexOf:@"/"]==0){
        return [NSString stringWithFormat:@"%@%@",hostname,url];
    }
    
    //if parentUrl has parameter ?
    //get last / before ?
    int posQuestion = [parentUrl indexOf:@"?"];
    if (posQuestion>=0){
        parentUrl = [parentUrl substringToIndex:posQuestion];
    }
    int position = [parentUrl lastIndexOf:@"/"];
    
    if (position==[parentUrl length]-1)
        return [NSString stringWithFormat:@"%@%@",parentUrl,url];
    
    if (position>6)
        return [NSString stringWithFormat:@"%@/%@",[parentUrl substringToIndex:position],url];
    
    return [NSString stringWithFormat:@"%@/%@",parentUrl,url];
    
    /*
    NSString *relative = hostname;
    parentUrl = [parentUrl trimEnd:@"/"];
    int position = [parentUrl lastIndexOf:@"/"];
    if (position>6)
        relative = [parentUrl substringToIndex:position];
    if ([url indexOf:@"/"]==0)
        return [NSString stringWithFormat:@"%@%@",hostname,url];
    return [NSString stringWithFormat:@"%@/%@",relative,url];
    */
}

+(NSString*) pathForResource:(NSString*)resourcepath directory:(NSString *)directoryStr
{
    NSBundle * mainBundle = [NSBundle mainBundle];
    NSMutableArray *directoryParts = [NSMutableArray arrayWithArray:[resourcepath componentsSeparatedByString:@"/"]];
    NSString       *filename       = [directoryParts lastObject];
    [directoryParts removeLastObject];
    
    NSString* directoryPartsJoined =[directoryParts componentsJoinedByString:@"/"];
    
    if ([directoryPartsJoined length] > 0) {
        directoryStr = [NSString stringWithFormat:@"%@/%@", directoryStr, [directoryParts componentsJoinedByString:@"/"]];
    }
    
    return [mainBundle pathForResource:filename ofType:@"" inDirectory:directoryStr];
}

//static void addRoundedRectToPath(CGContextRef context, CGRect rect, float ovalWidth, float ovalHeight)
+(void)addRoundedRectToPath:(CGContextRef)context rect:(CGRect)rect ovalWidth:(float)ovalWidth ovalHeight:(float)ovalHeight
{
    float fw, fh;
    if (ovalWidth == 0 || ovalHeight == 0) {
        CGContextAddRect(context, rect);
        return;
    }
    CGContextSaveGState(context);
    CGContextTranslateCTM (context, CGRectGetMinX(rect), CGRectGetMinY(rect));
    CGContextScaleCTM (context, ovalWidth, ovalHeight);
    fw = CGRectGetWidth (rect) / ovalWidth;
    fh = CGRectGetHeight (rect) / ovalHeight;
    CGContextMoveToPoint(context, fw, fh/2);
    CGContextAddArcToPoint(context, fw, fh, fw/2, fh, 1);
    CGContextAddArcToPoint(context, 0, fh, 0, fh/2, 1);
    CGContextAddArcToPoint(context, 0, 0, fw/2, 0, 1);
    CGContextAddArcToPoint(context, fw, 0, fw, fh/2, 1);
    CGContextClosePath(context);
    CGContextRestoreGState(context);
}
+(void)displayMessage:(NSString *)message withTitle:(NSString *)title{
    if (!title)
        title = @"Error";
    UIAlertView *av = [[UIAlertView alloc] initWithTitle:title message:message delegate:nil cancelButtonTitle:@"Cancel" otherButtonTitles:nil];
    [av show];
}
@end

@implementation NSString (CSharp)
-(int)indexOf:(NSString *)substring{
    if ([substring length]==0)
        return 0;
    NSRange range = [self rangeOfString:substring];
    if(range.location!=NSNotFound)
        return range.location;
    return -1;
}
-(int)indexOf:(NSString *)substring fromIndex:(int)index{
    if (index > [self length]-1)
        return -1;
    NSRange r = NSMakeRange(index, [self length]-index);
    NSRange range = [self rangeOfString:substring options:NSLiteralSearch range:r];
    if (range.location!=NSNotFound)
        return range.location;
    return -1;
}
-(int)lastIndexOf:(NSString *)substring{
    if ([substring length]==0)
        return 0;
    NSRange range = [self rangeOfString:substring options:NSBackwardsSearch];
    if (range.location!=NSNotFound)
        return range.location;
    return -1;
}
-(NSString *)trimEnd:(NSString *)st{
    NSRange range = [self rangeOfString:st options:NSBackwardsSearch];
    if (range.location!=NSNotFound && range.location+range.length==[self length])
        return [self stringByReplacingCharactersInRange:range withString:@""];
    return self;
}
-(NSString *)trimStart:(NSString *)st{
    NSRange range = [self rangeOfString:st];
    if (range.location==0)
        return [self stringByReplacingCharactersInRange:range withString:@""];
    return self;
}
- (NSString *)trimWhitespace {
    NSMutableString *mStr = [self mutableCopy];
    CFStringTrimWhitespace((__bridge CFMutableStringRef)mStr);
    NSString *result = [mStr copy];
    return result;
}

- (NSString *)substringBetween:(NSString *)st1 st2:(NSString *)st2{
    NSString *result = @"";
    int pos1 = [self indexOf:st1];
    int pos2 = 0;
    if (pos1>=0){
        pos2 = [self indexOf:st2 fromIndex:(pos1+[st1 length])];
        if (pos1>0 && pos2>0)
            result = [self substringWithRange:NSMakeRange(pos1+[st1 length], pos2-pos1-[st1 length])];
    }
    return result;
}

@end
